package com.tc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tc.dao.AdvertisingIMGMapper;
import com.tc.dao.BusDetailsMapper;
import com.tc.dao.ComdTrucksMapper;
import com.tc.dao.CommodityMapper;
import com.tc.pojo.BusDetails;
import com.tc.pojo.Commodity;
import com.tc.service.BusDetailsService;
import lombok.val;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/****
 * @Author:admin
 * @Description:BusDetails业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class BusDetailsServiceImpl implements BusDetailsService {

    @Autowired
    private BusDetailsMapper BusDetailsMapper;

    @Autowired
    private CommodityMapper commodityMapper;


    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * BusDetails条件+分页查询
     * @param BusDetails 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<BusDetails> findPage(BusDetails BusDetails, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(BusDetails);
        //执行搜索
        return new PageInfo<BusDetails>(BusDetailsMapper.selectByExample(example));
    }

    /**
     * BusDetails分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<BusDetails> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<BusDetails>(BusDetailsMapper.selectAll());
    }

    /**
     * BusDetails条件查询
     * @param BusDetails
     * @return
     */
    @Override
    public List<BusDetails> findList(BusDetails BusDetails){
        //构建查询条件
        Example example = createExample(BusDetails);
        //根据构建的条件查询数据
        return BusDetailsMapper.selectByExample(example);
    }


    /**
     * BusDetails构建查询对象
     * @param BusDetails
     * @return
     */
    public Example createExample(BusDetails BusDetails){
        Example example=new Example(BusDetails.class);
        Example.Criteria criteria = example.createCriteria();
        if(BusDetails!=null){
            // 编号自增长
            if(!StringUtils.isEmpty(BusDetails.getId())){
                    criteria.andEqualTo("id",BusDetails.getId());
            }
            // 上首页时用的图片
            if(!StringUtils.isEmpty(BusDetails.getImg())){
                    criteria.andEqualTo("img",BusDetails.getImg());
            }
            // 视频
            if(!StringUtils.isEmpty(BusDetails.getMovie())){
                    criteria.andEqualTo("movie",BusDetails.getMovie());
            }
            // 遍历所有服务时用的小图
            if(!StringUtils.isEmpty(BusDetails.getTitleImg())){
                    criteria.andLike("titleImg","%"+BusDetails.getTitleImg()+"%");
            }
            // 头信息介绍
            if(!StringUtils.isEmpty(BusDetails.getTitle())){
                    criteria.andLike("title","%"+BusDetails.getTitle()+"%");
            }
            // 二级头信息介绍
            if(!StringUtils.isEmpty(BusDetails.getSectitle())){
                    criteria.andLike("sectitle","%"+BusDetails.getSectitle()+"%");
            }
            // 公司介绍
            if(!StringUtils.isEmpty(BusDetails.getCompany())){
                    criteria.andEqualTo("company",BusDetails.getCompany());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        BusDetailsMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改BusDetails
     * @param BusDetails
     */
    @Override
    public void update(BusDetails BusDetails){
        BusDetailsMapper.updateByPrimaryKey(BusDetails);
    }

    /**
     * 增加BusDetails
     * @param BusDetails
     */
    @Override
    public void add(BusDetails BusDetails){
        BusDetailsMapper.insert(BusDetails);
    }

    /**
     * 根据ID查询BusDetails
     * @param id
     * @return
     */
    @Override
    public BusDetails findById(Integer id){
        return  BusDetailsMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询BusDetails全部数据
     * @return
     */
    @Override
    public List<BusDetails> findAll() {
        return BusDetailsMapper.selectAll();
    }

    @Override
    public Map findProduct(Integer id) {
//        Map<String,Object> data=new HashMap();
        Map data = redisTemplate.opsForHash().entries(("BusDetails"+id));

        if (data == null || data.size() <= 0) {
            BusDetails details = BusDetailsMapper.findProduct(id);
            List<Commodity> commodities = commodityMapper.findCommodityBypid(id);
            redisTemplate.opsForHash().put(("BusDetails"+id),"commodity",commodities);
            redisTemplate.opsForHash().put(("BusDetails"+id),"BusDetails",details);

            data = redisTemplate.opsForHash().entries(("BusDetails"+id));
        }
            return data;
        }

}
